package com.hitgo.soccer;

import com.hitgo.soccer.config.ConfigManager;
import com.hitgo.soccer.database.CacheFactory;
import com.hitgo.soccer.database.DBManager;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionFilter implements Filter 
{
	public static final String DEFAULT_ENCODING = "UTF-8";

	public SessionFilter() 
	{
	}

	public void init(FilterConfig cfg) throws ServletException 
	{
		String path = cfg.getServletContext().getRealPath("/") + "WEB-INF/";
		ConfigManager.loadConfig(path);
		CacheFactory.init();
		DBManager.init();
		
	}

	public void destroy() 
	{
	}

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain) throws IOException, ServletException 
	{
		req.setCharacterEncoding("UTF-8");
		HttpServletRequest httpReq = (HttpServletRequest)req ;

		HttpSession session = httpReq.getSession();

		if(session.isNew() || session.getCreationTime() +5*60*1000 < System.currentTimeMillis()) 
		{
			// TODO: 进行平台验证
		}	
		
		filterChain.doFilter(req, res);
	}
}